<?php
/**
 * Created by PhpStorm.
 * User: wuchunyin
 * Date: 2018/5/18
 * Time: 17:00
 */

namespace app\index\controller;

use app\index\base_controller\BaseController;
use think\Db;
use think\Exception;
use think\facade\Session;

class Rule extends BaseController{

    public function ruleList(){
        return $this->fetch('rule_list');
    }

    public function add(){
        $parentRuleList = Db::table('MNG_GZ_PAGE')
            ->where(['GROUP_ID'=>'0'])
            ->order('DIS_ORDER asc')
            ->select();
        $this->assign('parentRuleList',$parentRuleList);
        return $this->fetch('rule_add');
    }

    public function addRule(){
        try{
            $data['PAGE_ID'] = input('PAGE_ID');
            $data['GROUP_ID'] = input('GROUP_ID');
            $data['PAGE_NAME'] = input('PAGE_NAME');
            $data['PAGE_URL'] = input('PAGE_URL');
            $data['IS_BASE'] = input('IS_BASE');
            Db::table('MNG_GZ_PAGE')->insert($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败：'.$e->getMessage()]);
        }
    }

    public function edit(){
        $id = input('id');
        $rule = Db::table('MNG_GZ_PAGE')->where(['PAGE_ID'=>$id])->find();
        if(empty($rule)){
            echo '查询权限信息失败';
            return;
        }
        $parentRuleList = Db::table('MNG_GZ_PAGE')
            ->where(['GROUP_ID'=>'0'])
            ->order('DIS_ORDER asc')
            ->select();
        $this->assign('parentRuleList',$parentRuleList);
        $this->assign('rule',$rule);
        return $this->fetch('rule_edit');
    }

    public function editRule(){
        try{
            $id = input('PAGE_ID');
            $data['GROUP_ID'] = input('GROUP_ID');
            $data['PAGE_NAME'] = input('PAGE_NAME');
            $data['PAGE_URL'] = input('PAGE_URL');
            $data['IS_BASE'] = input('IS_BASE');

            Db::table('MNG_GZ_PAGE')->where(['PAGE_ID'=>$id])->update($data);
            return json_encode(['code'=>'200','msg'=>'操作成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>'操作失败：'.$e->getMessage()]);
        }
    }

    public function getRuleList(){
        $limit = input('limit');
        $offset = input('offset');
        $search = strtolower(input('search'));
        $sort = input('sort');
        $order = input('order');
        $sortStr = "$sort $order";
        if(empty($sort)){
            $sortStr = 'PAGE_ID asc';
        }

        $condition = [];
        if (!empty($search)) {
            $condition[] = ['PAGE_NAME|PAGE_URL|REMARK|PAGE_ID','like',"%$search%"];
        }
        try{
            $list = Db::view('MNG_GZ_PAGE', '*')
                ->limit($offset, $limit)
                ->where($condition)
                ->order($sortStr)
                ->select();

            $total = Db::view('MNG_GZ_PAGE', '*')
                ->where($condition)
                ->count();

            $data['rows'] = $list;
            $data['total'] = $total;
            return $data;
        }
        catch (Exception $e){
            return $e->getMessage();
        }
    }

    public function deleteRule(){
        $id = input('id');
        try{
            Db::table('MNG_GZ_PAGE')->where(['PAGE_ID'=>$id])->delete();
            return json_encode(['code'=>'200','msg'=>'删除成功']);
        }
        catch (Exception $e){
            return json_encode(['code'=>'0','msg'=>$e->getMessage()]);
        }
    }
}